package com.huawei.knowledge.doc.service;

import com.huawei.knowledge.cfg.MyAppConfig;
import com.huawei.knowledge.doc.mapper.DocumentMapper;
import com.huawei.knowledge.doc.model.DocClassifyModel;
import com.huawei.knowledge.doc.model.DocumentDetailModel;
import com.huawei.knowledge.doc.model.DocumentModel;
import com.huawei.knowledge.doc.service.md.MarkToHtml;
import com.huawei.knowledge.utils.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.List;

/**
 * Created by Administrator on 2018/4/21/021.
 */
@Component(value = "documentService")
public class DocumentService {

    @Autowired
    private DocumentMapper documentMapper;

    @Autowired
    private MyAppConfig myAppConfig;

    public List<DocClassifyModel> queryDocClassify() {
        return documentMapper.queryDocClassifyList();
    }

    public List<DocumentModel> queryDocList(String classifyIndex) {
        return documentMapper.queryDocList(classifyIndex);
    }

    /**
     * 返回文档首页内容
     *
     * @param classifyIndex 分类索引
     * @param docIndex      文档索引
     * @return 文档内容
     */
    public DocumentDetailModel showDocumentIndexPage(String classifyIndex, String docIndex) {
        //文档默认路径为 目录映射 + classifyIndex + documentIndex
        String documentPath = myAppConfig.getRootpath() + File.separator + classifyIndex + File.separator + docIndex;

        //读取默认的文档目录配置文件cfg.json获取文档目录\
        String dirStructureFilePath = documentPath + File.separator + "cfg.json";
        String dirStructure = FileUtils.readFileContent(dirStructureFilePath);

        //默认展示文档默认路径下的readme.md文件
        String readmeFilePath = documentPath + File.separator + "readme.md";
        String readmeContent = FileUtils.readFileContent(readmeFilePath);
        MarkToHtml markToHtml = new MarkToHtml(readmeContent, documentPath);
        String readmeHtml = markToHtml.mkToHtml();
//        PegDownProcessor pdp = new PegDownProcessor(Integer.MAX_VALUE);
//        String readmeHtml = pdp.markdownToHtml(readmeContent);
//        String readmeHtml =  MDTool.markdown2Html(readmeContent);
//        MarkdownProcessor markdownProcessor = new MarkdownProcessor();
//        String readmeHtml = markdownProcessor.markdown(readmeContent);

        DocumentDetailModel result = new DocumentDetailModel();
        result.setDirStructure(dirStructure);
        result.setReadmeHtml(readmeHtml);
        return result;
    }

    /**
     * 按页展示文档内容
     *
     * @param docDir      文档目录
     * @param docPageFile 单页文件路径
     * @return 文档内容 单页内容
     */
    public String showDocumentByPage(String docDir, String docPageFile) {
        //文档默认路径为 目录映射 + 文档相对路径
        String documentPath = myAppConfig.getRootpath() + File.separator + docDir;

        //默认展示文档默认路径下的readme.md文件
        String filePath = documentPath + File.separator + docPageFile;
        String fileContent = FileUtils.readFileContent(filePath);

        MarkToHtml markToHtml = new MarkToHtml(fileContent, documentPath);
        String fileContentHtml = markToHtml.mkToHtml();

        return fileContentHtml;
    }

    /**
     * 更新文档的浏览次数
     *
     * @param classifyIndex 文档分类
     * @param docIndex      文档索引
     */
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    public void upDateViewTimes(String classifyIndex, String docIndex) {
        //查询当前文档的浏览次数
        synchronized (this) {
            List<DocumentModel> docList = documentMapper.queryDoc(classifyIndex, docIndex);
            if (docList != null && !docList.isEmpty()) {
                int newViewTimes = docList.get(0).getBrowserTimes() + 1;
                // 更新
                documentMapper.updateDocViewTimes(classifyIndex, docIndex, newViewTimes);
            }
        }

    }
}
